//go:generate goversioninfo -icon=resource/img/favicon.ico
package main

import (
	"embed"
	"fmt"
	"github.com/gin-gonic/gin"
	"io/fs"
	"net/http"
	"os/exec"
	"runtime"
	"time"
)

//go:embed resource/*
var resource embed.FS

// TIP To run your code, right-click the code and select <b>Run</b>. Alternatively, click
// the <icon src="AllIcons.Actions.Execute"/> icon in the gutter and select the <b>Run</b> menu item from here.
func returnHtml(c *gin.Context, indexFile fs.File) {
	var buff []byte
	buff = make([]byte, 8192)
	for {
		num, _ := indexFile.Read(buff)
		c.Writer.Write(buff)
		if num < 8192 {
			break
		}
	}
}

func main() {
	//TIP Press <shortcut actionId="ShowIntentionActions"/> when your caret is at the underlined or highlighted text
	// to see how GoLand suggests fixing it.
	r := gin.Default()

	resourceCss, _ := fs.Sub(resource, "resource/css")
	resourceData, _ := fs.Sub(resource, "resource/data")
	resourceImg, _ := fs.Sub(resource, "resource/img")
	resourceJs, _ := fs.Sub(resource, "resource/js")
	resourceCard, _ := fs.Sub(resource, "resource/card")
	r.StaticFS("/css", http.FS(resourceCss))
	r.StaticFS("/data", http.FS(resourceData))
	r.StaticFS("/img", http.FS(resourceImg))
	r.StaticFS("/js", http.FS(resourceJs))
	r.StaticFS("/card", http.FS(resourceCard))

	r.GET("/", func(c *gin.Context) {
		indexFile, _ := resource.Open("resource/index.html")
		returnHtml(c, indexFile)
	})
	r.GET("/card_search.html", func(c *gin.Context) {
		indexFile, _ := resource.Open("resource/card_search.html")
		returnHtml(c, indexFile)
	})
	r.GET("/deck_create.html", func(c *gin.Context) {
		indexFile, _ := resource.Open("resource/deck_create.html")
		returnHtml(c, indexFile)
	})
	r.GET("/deck_edit.html", func(c *gin.Context) {
		indexFile, _ := resource.Open("resource/deck_edit.html")
		returnHtml(c, indexFile)
	})
	r.GET("/deck_edit2.html", func(c *gin.Context) {
		indexFile, _ := resource.Open("resource/deck_edit2.html")
		returnHtml(c, indexFile)
	})
	r.GET("/info.html", func(c *gin.Context) {
		indexFile, _ := resource.Open("resource/info.html")
		returnHtml(c, indexFile)
	})
	r.GET("/ip_select.html", func(c *gin.Context) {
		indexFile, _ := resource.Open("resource/ip_select.html")
		returnHtml(c, indexFile)
	})

	r.GET("/ping", func(c *gin.Context) {
		c.JSON(http.StatusOK, gin.H{
			"message": "ping",
		})
	})

	go func() {
		time.Sleep(time.Second * 5)

		var cmd *exec.Cmd
		var ip = "127.0.0.1"
		var port = 2345

		switch runtime.GOOS {
		case "windows":
			cmd = exec.Command("cmd", fmt.Sprintf("/c start http://%s:%d", ip, port))
		case "darwin":
			cmd = exec.Command("open", fmt.Sprintf("http://%s:%d", ip, port))
		case "linux":
			cmd = exec.Command("xdg-open", fmt.Sprintf("http://%s:%d", ip, port))
		}
		_ = cmd.Start()
	}()

	r.Run("0.0.0.0:2345") // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")
}

//TIP See GoLand help at <a href="https://www.jetbrains.com/help/go/">jetbrains.com/help/go/</a>.
// Also, you can try interactive lessons for GoLand by selecting 'Help | Learn IDE Features' from the main menu.
